#include "serialportconfig.h"
#include <QCoreApplication>
#include <QSettings>

// 默认数据处理
std::once_flag  SerialportConfig::m_initFlag;
std::unique_ptr<SerialportConfig> SerialportConfig::m_pInstance = nullptr;
SerialportConfig::SerialportConfig(QObject* parent)
    :QObject(parent)
{

}

// 析构方法
SerialportConfig::~SerialportConfig()
{

}

// 静态方法
SerialportConfig* SerialportConfig::instance()
{
    std::call_once(m_initFlag, [&]() {m_pInstance = std::unique_ptr<SerialportConfig>(new SerialportConfig()); });
    return m_pInstance.get();
}

// 数据获取
bool SerialportConfig::getSerialPortInfo(QVector<PSeriaPortInfo> &info)
{
    QMutexLocker locker(&serialInfoMutex);
    loadSerialPortInfo();
    info = m_vecSerPortInfo;
    return true;
}

// 加载数据
bool SerialportConfig::loadSerialPortInfo()
{
    m_vecSerPortInfo.clear();
    QString str = searchSystemConfigFile();
    QSettings *settings = new QSettings(str,QSettings::IniFormat);
    settings->setIniCodec(QTextCodec::codecForName("UTF_8"));
    settings->beginGroup("serialports");
    int serialports = settings->value("serialports","").toInt();
    settings->endGroup();

    for(int i=0;i<serialports;++i){
        settings->beginGroup("serialport"+QString::number(i));
        PSeriaPortInfo serInfo;
        serInfo.name = settings->value("name").toString();
        serInfo.comNO = settings->value("comNO").toString();
        serInfo.baudrate = settings->value("baudrate").toInt();
        serInfo.parity = settings->value("parity").toString();
        serInfo.databits = settings->value("databits").toInt();
        serInfo.stopbits = settings->value("stopbits").toString();
        m_vecSerPortInfo.push_back(serInfo);
        settings->endGroup();
    }

    delete settings;

    return true;
}

// 新数据赋值
bool SerialportConfig::setSerialPortInfo(const QVector<PSeriaPortInfo> info)
{
    QMutexLocker locker(&serialInfoMutex);
    m_vecSerPortInfo = info;
    saveSerialPortInfo();
    return true;
}

// 新数据保存
bool SerialportConfig::saveSerialPortInfo()
{
    QString str = searchSystemConfigFile();
    QSettings *settings = new QSettings(str,QSettings::IniFormat);
    settings->setIniCodec(QTextCodec::codecForName("UTF_8"));
    settings->beginGroup("serialports");
    settings->setValue("serialports",m_vecSerPortInfo.size());
    settings->endGroup();

    for (int i = 0; i < m_vecSerPortInfo.size(); i++){
        settings->beginGroup("serialport"+QString::number(i));
        settings->setValue("name",m_vecSerPortInfo[i].name);
        settings->setValue("comNO",m_vecSerPortInfo[i].comNO);
        settings->setValue("baudrate",m_vecSerPortInfo[i].baudrate);
        settings->setValue("parity",m_vecSerPortInfo[i].parity);
        settings->setValue("databits",m_vecSerPortInfo[i].databits);
        settings->setValue("stopbits",m_vecSerPortInfo[i].stopbits);
        settings->endGroup();
    }

    delete settings;

    return true;
}

// 读取配置文件
QString SerialportConfig::searchSystemConfigFile() {
    QString binDir= QCoreApplication::applicationDirPath();
    QString configName("sys_param.ini");
    QString fileName;
    // 搜索配置文件
    QString configStr;
    configStr = binDir+"/ini";
    QFile files(configStr+"/"+configName);
    if (files.exists())
    {
        fileName = QDir(files.fileName()).canonicalPath();
    }

    return fileName;
}

// 获取当前毫秒
QString SerialportConfig::getMilliseconds(QDateTime dataTime)
{
    // 获取当前时间
    QTime current_time = dataTime.time();
    int hour = current_time.hour();        //当前的小时
    int minute = current_time.minute();    //当前的分
    int second = current_time.second();    //当前的秒
    int msec = current_time.msec();        //当前的毫秒

    // 计算毫秒
    int number = hour*3600 + minute*60 + second; // 要转换的数字
    QTime time;
    time.setHMS(0, 0, 0); // 设置初始时间为0小时、0分钟、0秒
    time = time.addSecs(number); // 将秒数添加到时间中
    int milliseconds = time.msecsSinceStartOfDay(); // 获取从当天开始计算的毫秒数

    // 总毫秒数
    int allmilliseconds = milliseconds + msec;

    return QString::number(allmilliseconds);
}

// 读取配置参数路径
QString SerialportConfig::searchSettingConfigFile() {
    QString binDir= QCoreApplication::applicationDirPath();
    QString configName("Config_SeriaPort.ini");
    QString fileName;
    // 搜索配置文件
    QString configStr;
    configStr = binDir+"/ini";
    QFile files(configStr+"/"+configName);
    if (files.exists())
    {
        fileName = QDir(files.fileName()).canonicalPath();
    }

    return fileName;
}

// 页面参数获取
bool SerialportConfig::getSettingInfo(PSettingInfo &info)
{
    QMutexLocker locker(&serialInfoMutex);
    // 获取地址
    QString fileName = searchSettingConfigFile();
    QSettings *settings = new QSettings(fileName,QSettings::IniFormat);
    settings->setIniCodec(QTextCodec::codecForName("UTF_8"));

    // 获取疲劳驾驶参数
    PSfatigueInfo serInfo;
    settings->beginGroup("sfatigue");
    serInfo.fatigue = settings->value("fatigue").toInt();
    serInfo.gaze_round = settings->value("gaze_round").toInt();
    serInfo.no_portrait = settings->value("no_portrait").toInt();
    serInfo.speeding = settings->value("speeding").toInt();
    serInfo.warnType = settings->value("warnType").toString();
    info.m_psfatigueInfo = serInfo;
    settings->endGroup();

    // 获取车辆状态参数
    PSvehicleInfo svehicleInfo;
    settings->beginGroup("sfatigue");
    svehicleInfo.fatigue = settings->value("fatigue").toInt();
    svehicleInfo.gaze_round = settings->value("gaze_round").toInt();
    svehicleInfo.no_portrait = settings->value("no_portrait").toInt();
    svehicleInfo.speeding = settings->value("speeding").toInt();
    info.m_psvehicleInfo = svehicleInfo;
    settings->endGroup();

    // 获取货物位移参数
    PSdriftInfo sdriftInfo;
    settings->beginGroup("sfatigue");
    sdriftInfo.fatigue = settings->value("fatigue").toInt();
    sdriftInfo.gaze_round = settings->value("gaze_round").toInt();
    sdriftInfo.no_portrait = settings->value("no_portrait").toInt();
    sdriftInfo.speeding = settings->value("speeding").toInt();
    info.m_psdriftInfo = sdriftInfo;
    settings->endGroup();

    delete settings;
    return true;
}

// 页面参数保存
bool SerialportConfig::setSettingInfo(const PSettingInfo info, const QString type)
{
    QMutexLocker locker(&serialInfoMutex);
    // 获取地址
    QString fileName = searchSettingConfigFile();
    QSettings *settings = new QSettings(fileName,QSettings::IniFormat);
    settings->setIniCodec(QTextCodec::codecForName("UTF_8"));

    // 保存疲劳驾驶
    if(type == "10001"){
        PSfatigueInfo m_sfatigueInfo = info.m_psfatigueInfo;
        settings->beginGroup("sfatigue");
        settings->setValue("fatigue",m_sfatigueInfo.fatigue);
        settings->setValue("gaze_round",m_sfatigueInfo.gaze_round);
        settings->setValue("no_portrait",m_sfatigueInfo.no_portrait);
        settings->setValue("speeding",m_sfatigueInfo.speeding);
        settings->setValue("warnType",m_sfatigueInfo.warnType);
        settings->endGroup();
    }else if(type == "10002"){
        PSvehicleInfo m_svehicleInfo = info.m_psvehicleInfo;
        settings->beginGroup("sfatigue");
        settings->setValue("fatigue",m_svehicleInfo.fatigue);
        settings->setValue("gaze_round",m_svehicleInfo.gaze_round);
        settings->setValue("no_portrait",m_svehicleInfo.no_portrait);
        settings->setValue("speeding",m_svehicleInfo.speeding);
        settings->endGroup();
    }else if(type == "10003"){
        PSdriftInfo m_sdriftInfo = info.m_psdriftInfo;
        settings->beginGroup("sfatigue");
        settings->setValue("fatigue",m_sdriftInfo.fatigue);
        settings->setValue("gaze_round",m_sdriftInfo.gaze_round);
        settings->setValue("no_portrait",m_sdriftInfo.no_portrait);
        settings->setValue("speeding",m_sdriftInfo.speeding);
        settings->endGroup();
    }

    delete settings;
    return true;
}

// 系统参数获取
bool SerialportConfig::getSysParamInfo(PSysparamInfo &info)
{
    QMutexLocker locker(&serialInfoMutex);
    // 获取地址
    QString fileName = searchSystemConfigFile();
    QSettings *settings = new QSettings(fileName,QSettings::IniFormat);
    settings->setIniCodec(QTextCodec::codecForName("UTF_8"));

    // 获取udp通讯协议参数
    PSysparamInfo serInfo;
    settings->beginGroup("udp");
    serInfo.sremote_udp_ip = settings->value("sremote_udp_ip").toString();
    serInfo.slocal_udp_ip = settings->value("slocal_udp_ip").toString();
    serInfo.sudp_port = settings->value("sudp_port").toInt();
    settings->endGroup();

    // 获取串口设备参数
    settings->beginGroup("serial");
    serInfo.svehicle_serial = settings->value("svehicle_serial").toString();
    serInfo.sdrift_serial = settings->value("sdrift_serial").toString();
    serInfo.sfatigue_serial = settings->value("sfatigue_serial").toString();
    serInfo.sposition_serial = settings->value("sposition_serial").toString();
    settings->endGroup();

    // 获取报警车辆参数
    settings->beginGroup("alarm");
    serInfo.cplate = settings->value("cplate").toString();
    serInfo.marchnum = settings->value("marchnum").toString();
    settings->endGroup();

    info = serInfo;
    delete settings;
    return true;
}
